package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ItemVoteTitleBinding;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.event.QuizDetailItemClickEvent;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;

/**
 * Created by Administrator on 2016/9/7.
 */
public class QuizVoteTitleViewHolder extends AbsViewHolder<QuizDetailItem> {
    private ItemVoteTitleBinding mBinding;

    public QuizVoteTitleViewHolder(View convertView) {
        super(convertView);
        mBinding = DataBindingUtil.bind(convertView);
        convertView.setOnClickListener(this);
    }

    @Override
    protected void onBindView(final QuizDetailItem item) {
        int progressDrawable = item.isUserVote ? R.drawable.vote_ratio_checked_color : R.drawable.vote_ratio_color;
        mBinding.tvName.setText(StringUtils.filterNullAndTrim(item.planName));
        mBinding.tvName.setChecked(item.isUserVote);
        mBinding.tvNum.setText(String.valueOf(item.planVoteCount));
        mBinding.divider.setVisibility(item.voteIndex==item.voteOptionCount-1?View.GONE:View.VISIBLE);
        mBinding.voteRatio.setProgress(item.voteCount == 0 ? 0 : (item.planVoteCount * 100 / item.voteCount));
        mBinding.voteRatio.setProgressDrawable(progressDrawable);
        mBinding.tvName.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!item.isUserVote) {//todo && !item.isCurrentUser
                    QuizDetailEvent event = new QuizDetailEvent(QuizDetailEvent.EVENT_USER_RE_TAKE_VOTE);
                    event.reOptionId = item.optionId;
                    EventBusUtil.post(event);
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        EventBusUtil.post(new QuizDetailItemClickEvent(item));
    }
}
